package com.sparrows.pirates.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.sparrows.pirates.SparrowPiratesGame;
import com.sparrows.pirates.model.NavalBattleMap;
import com.sparrows.pirates.model.Ship;

public class NavalBattleScreen implements Screen {
	SparrowPiratesGame game;
	OrthographicCamera camera;
	
	int mapWidth = 32;
	int mapHeight = 32;
	int tileSize = 64;
	
	Texture textureSheet;
	TextureRegion playerTex, cannonBall, smokeSheet, enemyTex;
	
	Ship player;
	NavalBattleMap map;

	public NavalBattleScreen(SparrowPiratesGame gam, float diff, int numEnemies, int batType){
		
		game = gam; //Global game variable for spritebatch and other things like that
		
		camera = new OrthographicCamera(960,540);
		camera.position.set((tileSize*mapWidth)/2, (tileSize*mapHeight)/2, 0);
		
		CreateTextures();

		//Create map
		map = new NavalBattleMap(batType, mapWidth, mapHeight, tileSize, textureSheet);
		map.create();
		
		//Create player in center of map
		player = new Ship("Player Ship", new Vector2((tileSize*mapWidth)/2, (tileSize*mapHeight)/2), playerTex);
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		update(delta);
		
		game.spriteBatch.begin();
		
		map.draw(game.spriteBatch);
		player.draw(game.spriteBatch);
		
		game.spriteBatch.end();
	}
	
	public void CreateTextures(){
		//Get textures
		textureSheet = game.manager.get("textures/textureSheetSmall.png");
		playerTex = new TextureRegion(textureSheet, 128, 0, 64, 64);
		cannonBall = new TextureRegion(textureSheet, 0, 64, 4, 4);
		enemyTex = new TextureRegion(textureSheet, 194, 0, 64, 64);
		
		//Grab the 7 frames of cannon fire
		smokeSheet = new TextureRegion(textureSheet, 1, 80, 189, 16);
		
	}
	
	public void update(float delta){
		ProcessInput();
		
		camera.update();
		player.update(delta, map.getBoundingBoxes(), map.getBufferZone());
		
		
		Vector2 playerPos = player.getPosition();
		//Set x position of camera as long as it's in bounds of the map
		if (playerPos.x >= 480 && playerPos.x <= (mapWidth*tileSize - 480))
			camera.position.set(playerPos.x, camera.position.y,0);
		//Set y position of camera
		if (playerPos.y >= 480 && playerPos.y <= (mapHeight*tileSize-480))
			camera.position.set(camera.position.x, playerPos.y, 0);
		
		game.spriteBatch.setProjectionMatrix(camera.combined);
	}
	
	public void ProcessInput(){
		//Turn left and right
		if (Gdx.input.isKeyPressed(Keys.LEFT)||Gdx.input.isKeyPressed(Keys.A))
			player.turn(true, true);
		else if (Gdx.input.isKeyPressed(Keys.RIGHT)||Gdx.input.isKeyPressed(Keys.D))
			player.turn(true, false);
		else
			player.turn(false, false);
		
		//Speed up or slow down
		if (Gdx.input.isKeyPressed(Keys.UP)||Gdx.input.isKeyPressed(Keys.W))
			player.changeSpeed(true);
		else if (Gdx.input.isKeyPressed(Keys.DOWN)||Gdx.input.isKeyPressed(Keys.S))
			player.changeSpeed(false);
		
		//Fire cannonball
		if (Gdx.input.isKeyPressed(Keys.SPACE)) {
			player.fireCannons(cannonBall, smokeSheet);
		}
	}
	
	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
}
